package com.alander.nexusgo.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.alander.nexusgo.user.model.entity.UserFollows;
import com.alander.nexusgo.user.service.UserFollowsService;
import com.alander.nexusgo.user.mapper.UserFollowsMapper;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

/**
 * @author alander
 */
@Service
public class UserFollowsServiceImpl extends ServiceImpl<UserFollowsMapper, UserFollows>
        implements UserFollowsService {

    @Override
    public boolean followUser(Long followerId, Long followedId) {
        // 检查是否已经关注
        if (isFollowing(followerId, followedId)) {
            return true; // 已经关注，直接返回成功
        }

        // 创建关注关系
        UserFollows userFollows = new UserFollows();
        userFollows.setFollowerId(followerId);
        userFollows.setFollowedId(followedId);
        userFollows.setFollowTime(new Date());
        userFollows.setStatus("1");

        return this.save(userFollows);
    }

    @Override
    public boolean unfollowUser(Long followerId, Long followedId) {
        // 查询关注关系
        LambdaQueryWrapper<UserFollows> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserFollows::getFollowerId, followerId)
                .eq(UserFollows::getFollowedId, followedId)
                .eq(UserFollows::getStatus, "1");

        UserFollows userFollows = this.getOne(queryWrapper);
        if (userFollows == null) {
            return true; // 不存在关注关系，直接返回成功
        }

        // 更新状态为取消关注
        userFollows.setStatus("0"); // 0表示已取消
        return this.updateById(userFollows);
    }

    @Override
    public boolean isFollowing(Long followerId, Long followedId) {
        LambdaQueryWrapper<UserFollows> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserFollows::getFollowerId, followerId)
                .eq(UserFollows::getFollowedId, followedId)
                .eq(UserFollows::getStatus, "1");

        return this.count(queryWrapper) > 0;
    }

    @Override
    public List<Long> getFollowingList(Long followerId) {
        LambdaQueryWrapper<UserFollows> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserFollows::getFollowerId, followerId)
                .eq(UserFollows::getStatus, "1")
                .orderByDesc(UserFollows::getFollowTime);
        List<UserFollows> list = this.list(queryWrapper);
        return list.stream().map(UserFollows::getFollowedId).toList();
    }

    @Override
    public List<UserFollows> getFollowersList(Long followedId) {
        LambdaQueryWrapper<UserFollows> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserFollows::getFollowedId, followedId)
                .eq(UserFollows::getStatus, "1")
                .orderByDesc(UserFollows::getFollowTime);

        return this.list(queryWrapper);
    }

    @Override
    public long getFollowingCount(Long userId) {
        LambdaQueryWrapper<UserFollows> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserFollows::getFollowerId, userId)
                .eq(UserFollows::getStatus, "1");

        return this.count(queryWrapper);
    }

    @Override
    public long getFollowersCount(Long userId) {
        LambdaQueryWrapper<UserFollows> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserFollows::getFollowedId, userId)
                .eq(UserFollows::getStatus, "1");

        return this.count(queryWrapper);
    }
}
